import { Body, Controller, Delete, Get, Param, ParseArrayPipe, ParseIntPipe, Post, Put, Query, Req, Res } from '@nestjs/common';
import { ParseIntArrayPipe } from '@/common/pipe/parse-int-array.pipe';
import Result from '@/common/result/Result';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { RequirePermission } from '@/common/decorator/require-premission.decorator';
import { nowDateTime } from '@/common/utils';
import { RentalPaymentService } from './service/rental-payment.service';
import { QueryRentalPaymentDto, CreateRentalPaymentDto, UpdateRentalPaymentDto } from './dto/index';
import { Response } from 'express';
import { RentalPayment } from '@prismaClient';
import { TableDataInfo } from '@/common/domain/TableDataInfo';
@ApiTags("费用表")
@ApiBearerAuth()
@Controller('system/payment')
export class RentalPaymentController {
    constructor(private paymentService: RentalPaymentService) { }
    @ApiOperation({ summary: "查询费用表列表" })
    @ApiResponse({ type: TableDataInfo<RentalPayment> })
    @RequirePermission("system:payment:list")
    @Get("/list")
    async listPayment(
      @Query() q: QueryRentalPaymentDto, @Req() req
    ): Promise<TableDataInfo<RentalPayment>> {
      const userId = req.userId;
       return Result.TableData(await this.paymentService.selectPaymentList(q, userId))
    }
    @ApiOperation({ summary: "查询费用表所有" })
    @ApiResponse({ type: Result<RentalPayment[]> })
    @RequirePermission("system:payment:list")
    @Get("/data")
    async listPaymentData(): Promise<Result<RentalPayment[]>> {
       return Result.ok(await this.paymentService.selectPaymentAll())
    }
    @ApiOperation({ summary: "导出费用表xlsx文件" })
    @RequirePermission("system:payment:export")
    @Get("/export")
    async export(@Res() res: Response): Promise<void> {
       return this.paymentService.exportPayment(res)
    }
    @ApiOperation({ summary: "查询费用表详细" })
    @ApiResponse({ type: Result<RentalPayment> })
    @RequirePermission("system:payment:list")
    @Get("/:id")
    async getPayment(@Param("id", ParseIntPipe) id: number): Promise<Result<RentalPayment>> {
        return Result.ok(await this.paymentService.selectPaymentById(id))
    }
    @ApiOperation({ summary: "新增费用表" })
    @ApiResponse({ type: Result<RentalPayment> })
    @ApiBody({ type: CreateRentalPaymentDto })
    @RequirePermission("system:payment:add")
    @Post("/")
    async addPayment(@Body() rentalPayment: CreateRentalPaymentDto, @Req() req): Promise<Result<RentalPayment>> {
rentalPayment["createTime"] = nowDateTime()
rentalPayment["updateTime"] = nowDateTime()
return Result.ok(await this.paymentService.addPayment(rentalPayment))
    }
   @ApiOperation({ summary: "修改费用表" })
   @ApiResponse({ type: Result<any> })
   @ApiBody({ type: UpdateRentalPaymentDto })
   @RequirePermission("system:payment:edit")
   @Put("/")
    async updatePayment(@Body() rentalPayment: UpdateRentalPaymentDto, @Req() req): Promise<Result<any>> {
rentalPayment["updateTime"] = nowDateTime()
await this.paymentService. updatePayment(rentalPayment)
        return Result.ok("修改成功！")
    }
    @ApiOperation({ summary: "删除费用表" })
    @ApiResponse({ type: Result<any> })
    @RequirePermission("system:payment:remove")
    @Delete("/:ids")
    async delPayment(@Param("ids", ParseIntArrayPipe) ids: number[]): Promise<Result<any>> {
        let { count, message } = await this.paymentService.deletePaymentByIds(ids)
        return Result.toAjax(count, message)
    }
}
